package tetrisbattle;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.util.HashMap;
import javax.swing.JPanel;
import javax.swing.BorderFactory;

/**
 *
 * @author Rick Jason G. Obrero
 */
public class TetrisBattleGrid extends JPanel{
    protected HashMap<Dimension, Integer> dimToInt = new HashMap<Dimension, Integer>();
    protected HashMap<Integer, Integer> intToBrick = new HashMap<Integer, Integer>();
    protected int rows, cols;
    Integer[][] gridArray;
    
    public TetrisBattleGrid(int rows, int cols){
        int i, j = 0, k = 0;
        
        this.rows = rows;
        this.cols = cols;
        
        JPanel[] panelSquares = new JPanel[rows*cols];
        gridArray = new Integer[rows][cols];
        
        this.setLayout(new GridLayout(this.rows, this.cols));
        
        for(i = 0; i < this.rows*this.cols; i++, k++){
            panelSquares[i] = new JPanel();
            panelSquares[i].setBackground(Color.BLACK);
            panelSquares[i].setBorder(BorderFactory.createLineBorder(Color.RED, 1));
            panelSquares[i].validate();
            this.add(panelSquares[i]);
            
            gridArray[j][k] = 0;
            this.dimToInt.put(new Dimension(j, k), i);
            /**
             * To randomize the color, uncomment the line and comment the line after.
             */
            //this.intToBrick.put(i, (int) ((Math.random() * 234) % 16) % 5);
            this.intToBrick.put(i, 0);
            if(k == 9){
                j++;
                k = -1;
            }
        }
        
        this.changeColor();
    }
    
    public void changeColor(){
        int i;
        for(int j = 0; j < rows; j++){
            for(int k = 0; k < cols; k++){
                i = this.dimToInt.get(new Dimension(j, k));
                switch(this.intToBrick.get(i)){
                    case 0: this.getComponent(i).setBackground(Color.BLACK); break;
                    case 1: this.getComponent(i).setBackground(Color.BLUE); break;
                    case 2: this.getComponent(i).setBackground(Color.GREEN); break;
                    case 3: this.getComponent(i).setBackground(Color.ORANGE); break;
                    case 4: this.getComponent(i).setBackground(Color.YELLOW); break;
                }
            }
        }
    }
    
    public void changeRowColor(int top, int bottom){
        for(int i = 0; i < this.cols; i++){
            int j = dimToInt.get(new Dimension(bottom, i));
            int k = intToBrick.get(dimToInt.get(new Dimension(top, i)));
            intToBrick.put(j, k);
        }
        changeColor();
    }
}
